// pages/server_end/withdrawals/withdrawals.js
const app = getApp();
const request = require('../../../utils/request.js');
const url = require('../../../utils/url.js');
const cookie = require('../../../utils/cookie.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    array: ['中国建设银行', '中国工商银行', '中国银行', '中国农业银行'],
    array1: ['建设银行徐汇支行', '建设银行徐汇支行', '建设银行徐汇支行', '建设银行徐汇支行'],
    index: 0,
    index1: 0,
    token: "",
    name: '',
    bankname: '',
    bankcard: '',
    money: '',
    can_money: '',
    checkPrivacy: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var that = this;
    cookie.getCookie('userInfo', function (res) {
      if (res) {
        var etime = Date.parse(new Date()) / 1000;
        
        if (etime < res.expiretime) {
          that.setData({
            login: 1,
            token: res.token
          })
          that.getWorkerInfo();
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  setAll() {
    this.setData({
      money: this.data.can_money
    })
  },
  changnanme: function (e) {
    var name = e.detail.value;
    this.setData({
      name: name
    })
  },
  changebankname: function (e) {
    var bankname = e.detail.value;
    this.setData({
      bankname: bankname
    })
  },
  changebankcard: function (e) {
    var bankcard = e.detail.value;
    this.setData({
      bankcard: bankcard
    })
  },
  changemoney: function (e) {
    var money = e.detail.value;
    this.setData({
      money: money
    })
  },
  getWorkerInfo: function () {
    var that = this;
    let param = {
      scoure: 'wxapp',
      token: that.data.token
    }
    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
    request.request(url.workerinfo, param, url.REQUEST_METHOD_GET).then((res) => {
      wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
      if (res.data.code == 1) {
        that.setData({
          can_money: res.data.data.can_money
        })
        //
      }
    })
  },
  apply() {
    var that = this;

    if (that.data.checkPrivacy == 0) {
      that.selectComponent('#xieyi').init();
      return false;
    }

    if (!that.data.name) {
      wx.showToast({
        title: '请输入姓名',
        icon: "none"
      })
      return false;
    }
    if (!that.data.bankname) {
      wx.showToast({
        title: '请输入开户行',
        icon: "none"
      })
      return false;
    }
    if (!that.data.bankcard) {
      wx.showToast({
        title: '请输入银行卡号',
        icon: "none"
      })
      return false;
    }
    if (that.data.money <= 0) {
      wx.showToast({
        title: '请输入正确的金额',
        icon: "none"
      })
      return false;
    }
    if (that.data.money > that.data.can_money) {
      wx.showToast({
        title: '不能超过可提现金额',
        icon: "none"
      })
      return false;
    }
    let param = {
      scoure: 'wxapp',
      token: that.data.token,
      name: that.data.name,
      bankcard: that.data.bankcard,
      bankname: that.data.bankname,
      money: that.data.money
    }
    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
    request.request(url.applywithdraw, param, url.REQUEST_METHOD_POST).then((res) => {
      wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 1) {

        setTimeout(function () {
          wx.navigateBack({
            delta: 1
          });
        }, 2000);
      }
    })
  },
})